package cn.com.surker.workcon.service.bcs;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.util.DateUtil;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.bcs.common.ProduceEnum;
import cn.com.surker.workcon.service.ProduceService;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.enums.production.invoice.InvoiceSendTypeEnum;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author luotao
 * @date 2024/12/18
 */
@Slf4j
public class ProduceServiceImpl extends GKJDBCService implements ProduceService {

	public Integer queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		//	查询sql
		String sql = "SELECT TOP 1 ID,ErpID,Code,Recipe,MorRec,DatTim,Vehicle,Driver,ProdMete,MorMete,TotVehs,TotMete,Qualitor,Acceptor,Attamper,Flag,Note,Station FROM Produce where erpid = " + dispatchId;
		List<Map<String, Object>> produceMaps = lineTemplate.queryForList(sql);
		if (produceMaps == null || produceMaps.isEmpty()) {
			return null;
		}
		//	取出工控查询的数据
		Map<String, Object> produceMap = produceMaps.get(0);
		Integer prostats = InvoiceProcessEnum.QUERN_RECEIVE.getCode();
		String flag = String.valueOf(produceMap.get("Flag"));
		if (StringUtils.isNotBlank(flag)) {
			flag = flag.toLowerCase();
			if (flag.indexOf(ProduceEnum.ACCEPT.getCode()) > -1) {
				//	等待生产
				prostats = InvoiceProcessEnum.QUERN_PRO.getCode();
				log.info("工控机接收了数据:查询的原始数据:{" + JSONObject.toJSONString(produceMap) + "},传入的参数:" + dispatchId + "操作时间:" + DateUtil.convertString(new Date()));
			}
			if (flag.indexOf(ProduceEnum.PRO.getCode()) > -1) {
				//	生产中
				prostats = InvoiceProcessEnum.PRO.getCode();
			}
			if (flag.indexOf(ProduceEnum.PRO_COMPLATE.getCode()) > -1) {
				//	生产完成
				prostats = InvoiceProcessEnum.PRO_COMPLETE.getCode();
			}
			if (flag.indexOf(ProduceEnum.PRO_STOP.getCode()) > -1) {
				//	生产中断
				prostats = InvoiceProcessEnum.PRO_COMPLETE.getCode();
			}
			if (flag.indexOf(ProduceEnum.WAIT_DEL.getCode()) > -1) {
				//	删除
				prostats = InvoiceProcessEnum.DEL.getCode();
			}
		}
		//	转换成erp数据
		/*ProductionIndentDispatch erpDisPatch = new ProductionIndentDispatch();
		erpDisPatch.setProStatus(prostats);*/
		return prostats;
	}

	@Override
	public Boolean disHasDel(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		String sql = "SELECT TOP 1 ID,ErpID,Code,Recipe,MorRec,DatTim,Vehicle,Driver,ProdMete,MorMete,TotVehs,TotMete,Qualitor,Acceptor,Attamper,Flag,Note,Station FROM Produce where erpid = " + dispatchId;
		List<Map<String, Object>> produceMaps = lineTemplate.queryForList(sql);
		if (produceMaps == null || produceMaps.isEmpty()) {
			return null;
		}
		//	取出工控查询的数据
		Map<String, Object> produceMap = produceMaps.get(0);
		String flag = String.valueOf(produceMap.get("Flag"));
		if (StringUtils.isNotBlank(flag)) {
			flag = flag.toLowerCase();
			if (flag.indexOf(ProduceEnum.WAIT_DEL.getCode()) > -1) {
				log.info("工控机删除了数据:查询的原始数据:{" + JSONObject.toJSONString(produceMap) + "},传入的参数:" + dispatchId + "操作时间:" + DateUtil.convertString(new Date()));
				//System.err.println("工控机删除了数据:查询的原始数据:{" + JSONObject.toJSONString(produceMap) + "},传入的参数:" + dispatchId + "操作时间:" + DateUtil.convertString(new Date()));
				return true;
			}
		}
		return false;
	}

	@Override
	public int insert(Long lineId, ProductionIndentDispatch dispatch) {
		if (lineId == null || dispatch == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	新增sql
		String sql = "insert into Produce (ErpID,Code,Recipe,MorRec,DatTim,Vehicle,Driver,ProdMete,MorMete,TotVehs,TotMete,Qualitor,Acceptor,Attamper,Flag,Note,Station) " +
			"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		//	转换数据
		Long station = line.getControlLineId();
		Long dispatchId = dispatch.getId();
		String indentCode = dispatch.getIndentCode();
		String recipe = dispatch.getRecipe();
		String morRec = "R" + recipe;
		Date datTime = dispatch.getUploadTime();
		String vehicle = dispatch.getCarCode();
		Integer uploadType = dispatch.getUploadType();
		Double uploadStere = dispatch.getUploadStere();
		Double concreteStere = 0d, sandStere = 0d;
		if (InvoiceSendTypeEnum.CONCRETE.getCode().equals(uploadType)) {
			concreteStere = uploadStere;
		} else if (InvoiceSendTypeEnum.SAND.getCode().equals(uploadType)) {
			concreteStere = uploadStere;
			sandStere = uploadStere;
		} else {
			//	默认为砼方量
			concreteStere = uploadStere;
		}
		Integer totalCar = dispatch.getTotalCar();
		Double totalStere = dispatch.getTotalStere();
		Object[] params = {dispatchId, indentCode, recipe, morRec, datTime, vehicle, "", concreteStere, sandStere, totalCar, totalStere, "", "", "", ProduceEnum.ERP_UPLOAD.getCode(), "", station};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int insert = lineTemplate.update(sql, params);
		return insert;
	}

	@Override
	public Integer delDispatch(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	删除sql
		String delSql = "delete from Produce where erpId = ? and flag = ?";
		Object[] params = {dispatchId, ProduceEnum.ERP_UPLOAD.getCode()};
		//	开启事务
		beginTransaction(lineId);
		int update = lineTemplate.update(delSql, params);
		return update;
	}
}


